package com.itheima.controller;

import com.itheima.dto.MediaMaterialDto;
import com.itheima.service.MaterialService;
import com.itheima.vo.MediaMaterialVo;
import com.itheima.vo.Result;
import com.itheima.vo.PagingQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author :wujunjie
 * createTime :2023-01-02 20:32
 * description :
 */
@RestController
@RequestMapping("/api/v1/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;


    @PostMapping("/upload_picture")
    public Result uploadPicture(MultipartFile multipartFile) throws IOException {
        MediaMaterialVo mediaMaterialVo=materialService.uploadPicture(multipartFile);
        return Result.success(mediaMaterialVo);
    }

    @GetMapping("/del_picture/{id}")
    public Result deletePicture(@PathVariable Long id)   {
        Boolean delete=materialService.deletePicture(id);
        if (delete){
            return Result.success();

        }
        return Result.error();
    }

    @PostMapping("/list")
    public Result selectPage(@RequestBody MediaMaterialDto dto){
        PagingQueryVo<MediaMaterialVo> data=materialService.selectPage(dto);
        return Result.mediaMaterialSuccess(data);
    }

    @GetMapping("/cancel_collect/{id}")
    public Result cancelCollection(@PathVariable(name = "id") Long id){
        Boolean result=materialService.cancelCollection(id);
        if (result){
            return Result.success();
        }
       return Result.error();
    }

    @GetMapping("/collect/{id}")
    public Result collect(@PathVariable(name = "id") Long id){
        Boolean result=materialService.collect(id);
        if (result){
            return Result.success();
        }
        return Result.error();
    }
}
